package org.zebias.springcloudeurekaclient.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.ribbon.proxy.annotation.Hystrix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.zebias.springcloudeurekaclient.entity.Customer;
import org.zebias.springcloudeurekaclient.service.SearchClient;

/**
 * @ClassName CustomController
 * @Description TODO
 * @Author zebias
 * @Date 2020/12/31 11:05
 * @Version 1.0
 **/
@RestController
public class CustomController {


    @Autowired
    private SearchClient searchClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/customer")
    public String customer(){
        return searchClient.search();
    }

    @GetMapping("/customer/{id}")
    @HystrixCommand(fallbackMethod = "findByIdFallBack",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.strategy",value = "THREAD"),
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
            @HystrixProperty(name="execution.isolation.strategy", value = "SEMAPHORE")
    })
    public Customer findById(@PathVariable("id") Integer id) throws InterruptedException {
        System.out.println(Thread.currentThread().getName());
            Thread.sleep(3000);
        return searchClient.findById(id);
    }

    /**
     * findById的降级方法，方法的描述要和接口一致
     * @param id
     * @return
     */
    public Customer findByIdFallBack(Integer id){
        return new Customer(-1,"",0);
    }

    @GetMapping("/getCustomer")
    public Customer getCustomer(@RequestParam Integer id, @RequestParam String name){
        return searchClient.getCustomer(id,name);
    }

    @PostMapping("/save")
    public Customer save(@RequestBody Customer customer){
        return searchClient.save(customer);
    }
}
